package com.lhy.service.impl;

import com.lhy.dao.DepartmentDao;
import com.lhy.dao.DoctorDao;
import com.lhy.dao.RegisterorderDao;
import com.lhy.domain.Department;
import com.lhy.domain.Doctors;
import com.lhy.domain.Registerorder;
import com.lhy.service.RegisterorderService;
import com.lhy.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 汪
 */
@Service
public class RegisterorderServiceImpl implements RegisterorderService {
    @Autowired
    RegisterorderDao registerorderDao;
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    DepartmentDao departmentDao;

    @Override
    public List<Registerorder> showAllRegisterorder() {
        List<Registerorder> registerorders = registerorderDao.showAllRegisterorder();
        for (Registerorder registerorder : registerorders) {
            Integer departId = registerorder.getDepartId();
            Integer docId = registerorder.getDocId();
            Department department = departmentDao.selectOneById(departId);
            Doctors doctors = doctorDao.getDoctorById(docId);
            registerorder.setDepartName(department.getName());
            registerorder.setDoctorName(doctors.getName());
        }
        return registerorders;
    }

    @Override
    public void deleteRegisterorder(Integer roId) {
        registerorderDao.deleteRegisterorder(roId);
    }

    @Override
    public void addRegisterorder(Registerorder registerorder) {
        registerorderDao.addRegisterorder(registerorder);
    }

    @Override
    public List<Registerorder> findSomeRegisterorder(Pagination<Registerorder> page) {
        String departName = page.getQueryObject().getDepartName();
        Department department1 = departmentDao.selectOneByName(departName);
        Integer departId1 = null;
        try {
            departId1 = department1.getDepartId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        page.getQueryObject().setDepartId(departId1);
        List<Registerorder> someRegisterorder = registerorderDao.findSomeRegisterorder(page);
        for (Registerorder registerorder : someRegisterorder) {
            Integer departId = registerorder.getDepartId();
            Integer docId = registerorder.getDocId();
            Department department = departmentDao.selectOneById(departId);
            Doctors doctors = doctorDao.getDoctorById(docId);
            registerorder.setDepartName(department.getName());
            registerorder.setDoctorName(doctors.getName());
        }

        return someRegisterorder;
    }
}
